package com.base.controller;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.base.comm.CommContent;
import com.base.po.voip.SelfShopCategory;
import com.base.po.voip.Users;
import com.base.service.LogsService;
import com.base.service.SelfShopCategoryService;
import com.base.service.UsersService;
import com.base.utils.IP;

@Controller
@RequestMapping("/admin")
public class update_SelfShopCategory {
	@Autowired
	private LogsService logsService;// log日志
	@Autowired
	private SelfShopCategoryService selfShopCategoryService;
	@Autowired
	private UsersService usersService;
	
	// 账户信息
	@RequestMapping("/update_SelfShopCategory")
	public ModelAndView index(HttpServletRequest request, @RequestParam(value = "id", required = true) String id)
			throws Exception {
		// 返回ModelAndView
		System.out.println(id);
		ModelAndView modelAndView = new ModelAndView();
		if (id == null) {
			modelAndView.setViewName("jsp/datanotfound");
			return modelAndView;
		}
		SelfShopCategory selfShopCategory = selfShopCategoryService.selectByPrimaryKey(id);
		if (selfShopCategory == null || selfShopCategory.getId() == null) {
			modelAndView.setViewName("jsp/datanotfound");
			return modelAndView;
		}

		String adminUserId = (String) request.getSession().getAttribute("adminUserId");

		request.setAttribute("selfShopCategory", selfShopCategory);

		if (adminUserId == null) {
			modelAndView.setViewName("jsp/login");
		} else {
			modelAndView.setViewName("jsp/update_SelfShopCategory");
		}
		return modelAndView;
	}
	
	@RequestMapping("/update_SecondCategory")
	public ModelAndView update_SecondCategory(HttpServletRequest request, @RequestParam(value = "id", required = true) String id)
			throws Exception {
		// 返回ModelAndView
		System.out.println(id);
		ModelAndView modelAndView = new ModelAndView();
		if (id == null) {
			modelAndView.setViewName("jsp/datanotfound");
			return modelAndView;
		}
		SelfShopCategory selfShopCategory = selfShopCategoryService.selectByPrimaryKey(id);
		if (selfShopCategory == null || selfShopCategory.getId() == null) {
			modelAndView.setViewName("jsp/datanotfound");
			return modelAndView;
		}

		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
		
		Users users = usersService.selectByPrimaryKey(adminUserId);
		List<SelfShopCategory> selfShopCategorys = null;
		//一级分类
		if(users.getRole() == 0 || users.getRole() == 1) {
			selfShopCategorys = selfShopCategoryService.selectByLevel1(adminUserId,adminUserId);
		}
		if(users.getRole() == 2) {
			selfShopCategorys = selfShopCategoryService.selectByLevel1(users.getParentid(),adminUserId);
		}
		
		request.setAttribute("selfShopCategory", selfShopCategory);
		request.setAttribute("selfShopCategorys", selfShopCategorys);

		if (adminUserId == null) {
			modelAndView.setViewName("jsp/login");
		} else {
			modelAndView.setViewName("jsp/update_SelfShopCategory");
		}
		return modelAndView;
	}

	String SUCCESS = "success";

	// 更改商户信息
	@RequestMapping(value = "/updateSelfShopCategory", method = RequestMethod.POST)
	public void updateSelfShopCategory(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(value = "id", required = true) String id, String pic, String category,
			String firstid) throws Exception {
		// 返回参数
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=UTF-8");
//		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
	
		SelfShopCategory selfShopCategory = selfShopCategoryService.selectByPrimaryKey(id);
		PrintWriter out = response.getWriter();

		if (id == null) {
			out.print("浏览器在点击的过程中发现丢失了用户信息，请您刷新重试！");
			return;
		} else {
			selfShopCategory.setPic(pic);
			selfShopCategory.setCategory(category);
			if(firstid != null && !firstid.equals("undefined")) {
				selfShopCategory.setTopid(firstid);
			}
			int code = selfShopCategoryService.updateByPrimaryKeySelective(selfShopCategory);
			String usernamesys = (String) request.getSession().getAttribute("useracc");
			if (code == 1) {
				logsService.insert(CommContent.TYPE_UPDATE, "修改" + category + "分类信息成功", usernamesys, true,
						IP.getIpAddr(request));
				out.print(SUCCESS);
			} else {
				logsService.insert(CommContent.TYPE_UPDATE, "修改" + category + "分类信息失败", usernamesys, true,
						IP.getIpAddr(request));
				out.print("更改失败，请您重试");
			}
		}
	}
	
	//删除时
	@RequestMapping(value = "/delectSelfShopCategory", method = RequestMethod.POST)
	public void updateShopinfoesTest(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(value = "id", required = true) String id, Byte datastatus) throws Exception {
		// 返回参数
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=UTF-8");
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
		
		Users users = usersService.selectByPrimaryKey(adminUserId);
		List<SelfShopCategory> selfShopCategory = null;
		if(users.getRole() == 0 || users.getRole() == 1) {
			selfShopCategory = selfShopCategoryService.selectByParentIDAndShopID(adminUserId,adminUserId);
		}
		if(users.getRole() == 2) {
			selfShopCategory = selfShopCategoryService.selectByParentIDAndShopID(users.getParentid(),adminUserId);
		}
		
		SelfShopCategory sp = selfShopCategoryService.selectByPrimaryKey(id);
		
		PrintWriter out = response.getWriter();
		
		
		for(SelfShopCategory s : selfShopCategory) {
			
			if(s.getGrade()< sp.getGrade()) {
				
			}else {
				s.setGrade(s.getGrade()-1);
				selfShopCategoryService.updateByPrimaryKeySelective(s);
			}
		}
		if (id == null) {
			out.print("浏览器在点击的过程中发现丢失了用户信息，请您刷新重试！");
			return;
		} else {
			sp.setDatastatus(datastatus);
			int code = selfShopCategoryService.updateByPrimaryKeySelective(sp);
			String usernamesys = (String) request.getSession().getAttribute("useracc");
			
				if (code == 1) {
					logsService.insert(CommContent.TYPE_UPDATE, "删除" + sp.getCategory() + "自营分类成功", usernamesys, true,
							IP.getIpAddr(request));
					out.print(SUCCESS);
				} else {
					logsService.insert(CommContent.TYPE_UPDATE, "修改" + sp.getCategory() + "自营分类失败", usernamesys, true,
							IP.getIpAddr(request));
					out.print("更改失败，请您重试");
				}
			

		}
	}
}
